/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xui_lib.component.textview.supertextview.adapter;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * 增强ShapeElement
 *
 * @since 2021-04-08
 */
public class OfShapeElement extends ShapeElement {
    private static final int INT_0 = 0;
    private static final int INT_1 = 1;
    private static final int INT_2 = 2;
    private static final int INT_3 = 3;

    /**
     * 无参构造
     */
    public OfShapeElement() {
        super();
    }

    /**
     * ResId狗在其
     *
     * @param context 上下文
     * @param xmlId ResId, 必须为Shape类型
     */
    public OfShapeElement(Context context, int xmlId) {
        super(context, xmlId);
    }

    /**
     * 设置填充颜色
     *
     * @param color 颜色
     */
    public void setColor(Color color) {
        super.setRgbColor(toRgbColor(color));
    }

    /**
     * 设置填充颜色
     *
     * @param color int类型
     */
    public void setColor(int color) {
        super.setRgbColor(toRgbColor(color));
    }

    /**
     * 设置圆角
     *
     * @param leftTop 左上
     * @param leftBottom 左下
     * @param rightTop 右上
     * @param rightBottom 右下
     */
    public void setCornerRadius(float leftTop, float leftBottom, float rightTop, float rightBottom) {
        setCornerRadiiArray(new float[]{
            leftTop, leftTop,
            rightTop, rightTop,
            rightBottom, rightBottom,
            leftBottom, leftBottom
        });
    }

    /**
     * 设置圆角
     *
     * @param radius 4个数值的数字据
     */
    public void setCornerRadius(float[] radius) {
        setCornerRadius(radius[INT_0], radius[INT_1], radius[INT_2], radius[INT_3]);
    }

    /**
     * 设置边框线
     *
     * @param width 宽度,单位px
     * @param color 颜色
     */
    public void setStroke(int width, Color color) {
        super.setStroke(width, toRgbColor(color));
    }

    /**
     * 设置边框和虚线
     *
     * @param width 宽度,单位px
     * @param color 颜色
     * @param dashWidth 虚线宽度
     * @param dashGap 间隙
     */
    public void setStroke(int width, Color color, float dashWidth, float dashGap) {
        this.setStroke(width, color);
        setDashPathEffectValues(new float[]{dashWidth, dashGap}, 0);
    }

    /**
     * 设置渐变色
     *
     * @param start 开始颜色
     * @param center 中间颜色
     * @param end 结束颜色
     */
    public void setColors(Color start, Color center, Color end) {
        setRgbColors(new RgbColor[]{toRgbColor(start), toRgbColor(center), toRgbColor(end)});
    }

    /**
     * 设置渐变色
     *
     * @param start 开始颜色
     * @param end 结束颜色
     */
    public void setColors(Color start, Color end) {
        setRgbColors(new RgbColor[]{toRgbColor(start), toRgbColor(end)});
    }

    private RgbColor toRgbColor(int color) {
        return RgbColor.fromArgbInt(color);
    }

    private RgbColor toRgbColor(Color color) {
        return RgbColor.fromArgbInt(color.getValue());
    }
}
